#include "AsyncAcceptor.h"

AsyncAcceptor::AsyncAcceptor( IMessageQueue *pMessageQueue )
: m_pMessageQueue( pMessageQueue )
{
}

AsyncAcceptor::~AsyncAcceptor(void)
{
}

int AsyncAcceptor::accept(size_t bytes_to_read, const void *act )
{
	return __super::accept( bytes_to_read, m_pMessageQueue );
}

/**
* Template method to validate peer before service is opened.
* This method is called after a new connection is accepted if the
* @a validate_connection argument to @c open() was non-zero or
* the @c validate_new_connection() method is called to turn this
* feature on.  The default implementation returns 0.  Users can
* reimplement this method to perform validation of the peer
* using it's address, running an authentication procedure (such as
* SSL) or anything else necessary or desireable. The return value
* from this method determines whether or not ACE will continue
* opening the service or abort the connection.
*
* @param result    Result of the connection acceptance.
* @param remote    Peer's address.
* @param local     Local address connection was accepted at.
*
* @retval  -1  ACE_Asynch_Acceptor will close the connection, and
*              the service will not be opened.
* @retval  0   Service opening will proceeed.
*/
int AsyncAcceptor::validate_connection (const ACE_Asynch_Accept::Result& result,
								 const ACE_INET_Addr &remote,
								 const ACE_INET_Addr& local)
{
	//ACE_SOCK_SEQPACK_Association ass(handle());
	//ass.get_remote_addr(m_RemoteAddress);
	int opt = 1;
	// Set Keepalive option;
	if( setsockopt((SOCKET)result.accept_handle(), SOL_SOCKET, SO_KEEPALIVE, (char*)&opt, sizeof(opt)) != SOCKET_ERROR )
	{
		/* Argument structure for SIO_KEEPALIVE_VALS */
		struct tcp_keepalive 
		{
			u_long  onoff;
			u_long  keepalivetime;
			u_long  keepaliveinterval;
		};

		tcp_keepalive inkeepalive = { 1, 5000, 5000 };
		tcp_keepalive outkeepalive;
		DWORD dwRet = 0;
		WSAIoctl( (SOCKET)result.accept_handle(), (IOC_IN|IOC_VENDOR|4), (LPVOID)&inkeepalive, sizeof(inkeepalive), (LPVOID)&outkeepalive, sizeof(outkeepalive), &dwRet, NULL, NULL );
	}
	return __super::validate_connection( result, remote, local );
}
